<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动名称" prop="activityName">
        <el-input
            v-model="queryParams.activityName"
            placeholder="请输入活动名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动状态" prop="activityStatus">
        <el-select v-model="queryParams.activityStatus" placeholder="请选择活动状态"
                   clearable>
          <el-option
              v-for="dict in train_activity_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['question:trainActivity:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['question:trainActivity:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['question:trainActivity:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['question:trainActivity:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Finished"
            :disabled="single"
            @click="handleBuildResult"
            v-hasPermi="['question:trainActivity:result']"
        >生成中奖信息
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trainActivityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="活动图" align="center" prop="activityIcon">
        <template #default="scope">
          <image-preview :src="scope.row.activityIcon" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="活动名称" width="200" show-overflow-tooltip align="center" prop="activityName"/>
      <el-table-column label="活动开始时间" align="center" prop="activityStart" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityStart, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="activityEnd" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityEnd, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动状态" align="center" prop="activityStatus">
        <template #default="scope">
          <dict-tag :options="train_activity_status" :value="scope.row.activityStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="金奖" align="center" width="200" show-overflow-tooltip prop="goldAwardName"/>
      <el-table-column label="金奖图片" align="center" prop="goldAwardIcon">
        <template #default="scope">
          <image-preview :src="scope.row.goldAwardIcon" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="银奖" align="center" width="200" show-overflow-tooltip prop="silverAwardName"/>
      <el-table-column label="银奖图片" align="center" prop="silverAwardIcon">
        <template #default="scope">
          <image-preview :src="scope.row.silverAwardIcon" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="铜奖" align="center" width="200" show-overflow-tooltip prop="bronzeAwardName"/>
      <el-table-column label="铜奖图片" align="center" prop="bronzeAwardIcon">
        <template #default="scope">
          <image-preview :src="scope.row.bronzeAwardIcon" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="240" fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              type="text"
              icon="Discount"
              @click="handleAddProblem(scope.row)"
              v-hasPermi="['question:trainActivity:edit']"
          >题目
          </el-button>
          <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['question:trainActivity:edit']"
          >修改
          </el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['question:trainActivity:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改答题活动对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" :close-on-click-modal="false" append-to-body>
      <el-form ref="trainActivityRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="请输入活动名称"/>
        </el-form-item>
        <el-form-item label="活动图" prop="activityIcon">
          <image-upload v-model="form.activityIcon" :limit="1"/>
        </el-form-item>
        <el-form-item label="活动开始时间" prop="activityStart">
          <el-date-picker clearable
                          v-model="form.activityStart"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="activityEnd">
          <el-date-picker clearable
                          v-model="form.activityEnd"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择活动结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动状态">
          <el-radio-group v-model="form.activityStatus">
            <el-radio
                v-for="dict in train_activity_status"
                :key="dict.value"
                :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="金奖" prop="goldAwardName">
          <el-input v-model="form.goldAwardName" placeholder="请输入金奖"/>
        </el-form-item>
        <el-form-item label="金奖图片" prop="goldAwardIcon">
          <image-upload v-model="form.goldAwardIcon" :limit="1"/>
        </el-form-item>
        <el-form-item label="银奖" prop="silverAwardName">
          <el-input v-model="form.silverAwardName" placeholder="请输入银奖"/>
        </el-form-item>
        <el-form-item label="银奖图片" prop="silverAwardIcon">
          <image-upload v-model="form.silverAwardIcon" :limit="1"/>
        </el-form-item>
        <el-form-item label="铜奖" prop="bronzeAwardName">
          <el-input v-model="form.bronzeAwardName" placeholder="请输入铜奖"/>
        </el-form-item>
        <el-form-item label="铜奖图片" prop="bronzeAwardIcon">
          <image-upload v-model="form.bronzeAwardIcon" :limit="1"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 管理题目对话框 -->
    <el-dialog title="活动题目管理" v-model="openProblem" :close-on-click-modal="false" width="70%" append-to-body>
      <div>
        <TrainActivityProblem :data="activityData"/>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="openProblem = false">确 定</el-button>
          <el-button @click="openProblem = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TrainActivity">
import {
  listTrainActivity,
  getTrainActivity,
  delTrainActivity,
  addTrainActivity,
  updateTrainActivity,
  buildResult
} from "@/api/question/trainActivity";
import TrainActivityProblem from "@/views/question/trainActivityProblem/index";

const {proxy} = getCurrentInstance();
const {train_activity_status} = proxy.useDict('train_activity_status');

const trainActivityList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    activityName: null,
    activityIcon: null,
    activityStart: null,
    activityEnd: null,
    activityStatus: null,
    goldAwardName: null,
    goldAwardIcon: null,
    silverAwardName: null,
    silverAwardIcon: null,
    bronzeAwardName: null,
    bronzeAwardIcon: null,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

// 题目管理窗口
const openProblem = ref(false);
const activityData = ref({});

/** 查询答题活动列表 */
function getList() {
  loading.value = true;
  listTrainActivity(queryParams.value).then(response => {
    trainActivityList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    activityName: null,
    activityIcon: null,
    activityStart: null,
    activityEnd: null,
    activityStatus: "0",
    goldAwardName: null,
    goldAwardIcon: null,
    silverAwardName: null,
    silverAwardIcon: null,
    bronzeAwardName: null,
    bronzeAwardIcon: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    delFlag: null
  };
  proxy.resetForm("trainActivityRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加答题活动";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTrainActivity(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改答题活动";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["trainActivityRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTrainActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTrainActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除答题活动编号为"' + _ids + '"的数据项？').then(function () {
    return delTrainActivity(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('question/trainActivity/export', {
    ...queryParams.value
  }, `trainActivity_${new Date().getTime()}.xlsx`)
}

/** 打开题目管理 */
function handleAddProblem(row) {
  activityData.value = row;
  openProblem.value = true;
}

/** 生成答题结果 */
function handleBuildResult(row) {
  const _id = row.id || ids.value
  buildResult(_id).then(res => {
    if (res.code === 200) {
      proxy.$modal.msgSuccess("生成答题结果成功");
    }
  });
}

getList();
</script>
